package use_LinkedList;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-04-15
 * Time: 16:05
 */
public class Test {
    public static void main(String[] args) {
        //构造一个空的LinkedList
        List<Integer> ListNode = new LinkedList<>();
        ListNode.add(10);
        ListNode.add(11);
        ListNode.add(111);
        ListNode.add(1111);

        System.out.println(ListNode.get(0));

        List<Integer> list1 = new ArrayList<>();
        list1.add(100);

        // 使用ArrayList构造LinkedList
        List<Integer> list2 = new LinkedList<>(list1);
        System.out.println(list2.get(0));

        //利用foreach进行遍历
        for (int x : ListNode ) {
            System.out.print(x + " ");
        }
        System.out.println();

        System.out.println("======直接利用引用进行遍历======");
        System.out.println(ListNode);
        System.out.println("======迭代器--逆序遍历======");

        //利用迭代器进行遍历-----反向遍历
        ListIterator<Integer> lit = ListNode.listIterator(ListNode.size());
        while (lit.hasPrevious()) {
            System.out.print(lit.previous() + " ");
        }
        System.out.println();

        System.out.println("======迭代器--正序遍历======");

        //利用迭代器进行遍历-----正向遍历
        ListIterator<Integer> lit2 = ListNode.listIterator();
        while(lit2.hasNext()) {
            System.out.print(lit2.next() + " ");
        }
        System.out.println();
    }
}
